<?php
	/**
	* Save request
	* @author trungdt
	* @since Nov 7  
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');
	$config = $db->selectObject('requestmodule_config',"location_data='".serialize($loc)."'");
	if (!$config)
	{
		die();
		$config->not_login_message="Not Configured";
		$config->submit_success_message="Not Configured";
	}
	$rs_request=null;
	$rs_request = requestmodule_request::publicrequest_save($_POST, $rs_request);
	$rs_request->location_data = serialize($loc);
	$rs_request->id=$db->insertObject($rs_request,"requestmodule_request");
	
		// get category id
	$cat=$db->selectObject("category", "id = {$rs_request->cat_id}");

	$status="Chờ xác nhận";
	if ($rs_request->approve == 2)
		$status="Đã xác nhận";
	// replace
	$search=array("%name%", 
	"%email%",
	"%phone%",
	"%from_date%",
	"%time_length%",
	"%note%",
	"%post_date%",
	"%ip%",
	"%id%",
	"%price%",
	"%cat_name%",
	"%status%"
	);
	$replace=array($rs_request->fullname,
	$rs_request->email,
	$rs_request->phone, 
	_ab_dateFormat($rs_request->from_date, false, true),
	$rs_request->time_length,
	$rs_request->content,
	_ab_dateFormat($rs_request->request_date, false, true),
	$_SERVER['REMOTE_ADDR'],
	$rs_request->id,
	$_REQUEST['tien_tam_tinh'],
	$cat->name,
	$status
	);
	
	// auto add to calendar
	if ($rs_request->approve == 2)
	{
		// build loc for calendar source
		$cloc=null;
		$cloc->mod="calendarmodule";
		$cloc->src=$config->calendar_src;
		$cloc->int="";
		$cloc=serialize($cloc);

		// build new calendar object
		$item=$rs_request;
		$event=null;
		$event->location_data = $cloc;
		$event->title=$item->fullname;
		$event->body=requestmodule::_BuildBody_for_calendar($rs_request);
		$event->eventstart=$item->from_date;
		$event->eventend=$item->from_date+$item->time_length*60;
		$event->posted=aboid_getCurrentTime();
		$event->poster=$user->id;
		$event->approve=2;
		$event->approver="{$user->firstname} {$user->lastname} ({$user->username})";
		$event->approve_date=aboid_getCurrentTime();
		$event->repeat_type=-1;
		$event->eventstart_day=date("j", $event->eventstart);
		$event->eventstart_month=date("n", $event->eventstart);
		$event->category_id=$item->cat_id;
		$rs_request->event_id=$db->insertObject($event,"calendar");
		
		// update event_id to request
		$db->updateObject($rs_request,"requestmodule_request");
	}



	// Send email,...
	// send email to admin 
	$headers['From'] =  SMTP_FROMADDRESS ;
	$headers['Reply-to'] = SMTP_FROMADDRESS;

	require_once(BASE.'subsystems/mail.php');
	$mail = null;
	$mail = new exponentMail();

	$mail->addHeaders($headers);
	$mail->addFrom(SMTP_FROMADDRESS, SITE_TITLE );
	$mail->addTo($_REQUEST['email']);

	

	$subject=$config->mail_subject;
	$subject=str_replace($search, $replace, $subject);
	$mail->subject($subject);

	$content=$config->mail_content;
	$content=str_replace($search, $replace, $content);
	$mail->addHTML($content);

	try{
		@$mail->send();
	}
	catch (Exception $e){

	}

	// send to admin
	// clear to property first
	$mail->to=null;
	$mail->addTo(SMTP_FROMADDRESS);
	$mail->subject("Chào Admin, có yêu cầu đặt thuê phòng từ {$rs_request->fullname}");
	try{
		@$mail->send();
	}
	catch (Exception $e){

	}

	// unset somefields we dont need to 

	echo $config->submit_success_message;
?>